package com.example.demo.service;

import com.example.base.Result;
import com.example.demo.dao.OrderMapper;
import com.example.demo.entity.Orders;
import com.example.exception.BusinessException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description order
 * @author yixin
 * @date 2025-06-12
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Resource
	private OrderMapper orderMapper;


	@Override
	public Object insert(Orders order) {

		// valid
		if (order == null) {
			return new BusinessException("必要参数缺失");
        }

		orderMapper.insert(order);
        return Result.Ok();
	}


	@Override
	public Object delete(int id) {
		int ret = orderMapper.delete(id);
		return ret>0?Result.Ok(): new BusinessException();
	}


	@Override
	public Object update(Orders order) {
		int ret = orderMapper.update(order);
		return ret>0?Result.Ok():new BusinessException();
	}


	@Override
	public Orders load(int id) {
		return orderMapper.load(id);
	}


	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {

		List<Orders> pageList = orderMapper.pageList(offset, pagesize);
		int totalCount = orderMapper.pageListCount(offset, pagesize);

		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", pageList);
		result.put("totalCount", totalCount);

		return result;
	}

}